package com.nowcoder.code.hjnc;

public class NC109 {


    public int solve(char[][] grid) {
        int res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == '0') {
                    continue;
                }
                res++;
                dfs(grid, i, j);
            }
        }

        return res;

    }

    public void dfs(char[][] grid, int i, int j) {
        if (i == grid.length || i < 0 || j == grid[i].length || j < 0 || grid[i][j] == '0') {
            return;
        }
        grid[i][j] = '0';
        dfs(grid, i, j + 1);
        dfs(grid, i, j - 1);
        dfs(grid, i + 1, j);
        dfs(grid, i - 1, j);
    }

}
